package getsmart.project.activity;

import java.util.ArrayList;
import getsmart.project.R;
import getsmart.project.database.Bintang;
import getsmart.project.database.Profil_Stat;
import getsmart.project.main.Utilities;


import android.widget.ImageView;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;

public class TestActivityMembaca extends Activity{
	Bintang dbStar;
	Tes_Pelajaran tp;
	Profil_Stat data_profil;
	int indexContent = 0;
	Button content1;
	Button content2;
	Button content3;
	Button content4;
	int result = 0;
	int kelasAnak = 0;
	int nilaiAnak= 0;
	int jumlahSoal = 5;
	ImageView img_test_membaca;
	ImageButton img_btn_next;
	int indexDataButton = 0;
	int [] indexRandomGambar;
	int [] anakAnswer;
	int [] realAnswer;
	String [][] multipleData;
	String [] soal;
	public static String hasilTestMembaca = "";
	private static void getTypeStudy(String hasil){
		TestActivityMembaca.hasilTestMembaca = hasil;
	}

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_membaca);
        dbStar = new Bintang(this);
        data_profil = new Profil_Stat(this);
        byte[] image = Utilities.getBytes(BitmapFactory.decodeResource(getResources(), R.drawable.apel));
        byte[] image1 = Utilities.getBytes(BitmapFactory.decodeResource(getResources(), R.drawable.mangga));
        byte[] image2= Utilities.getBytes(BitmapFactory.decodeResource(getResources(), R.drawable.banana));
        byte[] image3= Utilities.getBytes(BitmapFactory.decodeResource(getResources(), R.drawable.anggur));
        byte[] image4= Utilities.getBytes(BitmapFactory.decodeResource(getResources(), R.drawable.jeruk));
        tp = new Tes_Pelajaran(this);
        anakAnswer = new int[jumlahSoal];
        multipleData = new String[jumlahSoal][4];
        ArrayList<ArrayList<Object>> dataTest = tp.getSoalTest(1, 1, 1, jumlahSoal);
        if(dataTest.size()==0){
        	tp.generateDataTest(image, image1, image2, image3, image4);
        }
    	soal = new String[dataTest.size()];
    	indexRandomGambar = new int[dataTest.size()];
    	realAnswer= new int[dataTest.size()];
        for(int i=0; i<dataTest.size();i++){
        	ArrayList<Object> baris = dataTest.get(i);
        	soal[i] = baris.get(2).toString();
        	indexRandomGambar[i] = Integer.parseInt(baris.get(0).toString());
        	multipleData[i][0] = baris.get(1).toString();
        	multipleData[i][1] = baris.get(2).toString();
        	multipleData[i][2] = baris.get(3).toString();
        	multipleData[i][3] = baris.get(4).toString();
        	realAnswer[i] = Integer.parseInt(baris.get(5).toString());
        }

        img_test_membaca = (ImageView) findViewById(R.id.img_test_membaca);
        content1 = (Button) findViewById(R.id.btnTestContent1);
        content1.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				content1.setBackgroundResource(R.drawable.clicktest);
				content1.setTextColor(Color.BLACK);
				content2.setBackgroundColor(Color.BLACK);
				content3.setBackgroundColor(Color.BLACK);
				content4.setBackgroundColor(Color.BLACK);
				content2.setTextColor(Color.WHITE);
				content3.setTextColor(Color.WHITE);
				content4.setTextColor(Color.WHITE);
				
				anakAnswer[indexDataButton] = 1;
			}
		});
        content2 = (Button) findViewById(R.id.btnTestContent2);
        content2.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				content2.setBackgroundResource(R.drawable.clicktest);
				anakAnswer[indexDataButton] = 2;
				content2.setTextColor(Color.BLACK);
				content1.setBackgroundColor(Color.BLACK);
				content3.setBackgroundColor(Color.BLACK);
				content4.setBackgroundColor(Color.BLACK);
				content1.setTextColor(Color.WHITE);
				content3.setTextColor(Color.WHITE);
				content4.setTextColor(Color.WHITE);
				
			}
		});
        content3 = (Button) findViewById(R.id.btnTestContent3);
        content3.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				content3.setBackgroundResource(R.drawable.clicktest);
				anakAnswer[indexDataButton] = 3;
				content3.setTextColor(Color.BLACK);
				content1.setBackgroundColor(Color.BLACK);
				content2.setBackgroundColor(Color.BLACK);
				content4.setBackgroundColor(Color.BLACK);
				content1.setTextColor(Color.WHITE);
				content2.setTextColor(Color.WHITE);
				content4.setTextColor(Color.WHITE);
			}
		});
        content4 = (Button) findViewById(R.id.btnTestContent4);
        content4.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				content4.setBackgroundResource(R.drawable.clicktest);
				anakAnswer[indexDataButton] = 4;
				content4.setTextColor(Color.BLACK);
				content1.setBackgroundColor(Color.BLACK);
				content2.setBackgroundColor(Color.BLACK);
				content3.setBackgroundColor(Color.BLACK);
				content1.setTextColor(Color.WHITE);
				content2.setTextColor(Color.WHITE);
				content3.setTextColor(Color.WHITE);
			}
		});		
        content1.setText(multipleData[indexDataButton][0]);
		content2.setText(multipleData[indexDataButton][1]);
		content3.setText(multipleData[indexDataButton][2]);
		content4.setText(multipleData[indexDataButton][3]);
        img_btn_next = (ImageButton) findViewById(R.id.img_btn_next_baca_test);
        img_btn_next.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {


				// TODO Auto-generated method stub				
				kelasAnak = ChooseKelas.kelasSiswa;
				img_test_membaca.setImageBitmap(Utilities.getImage(tp.getImageFromDatabase(indexRandomGambar[indexContent])));
				content1.setTextColor(Color.WHITE);
				content2.setTextColor(Color.WHITE);
				content3.setTextColor(Color.WHITE);
				content4.setTextColor(Color.WHITE);				
				content1.setBackgroundColor(Color.BLACK);
				content2.setBackgroundColor(Color.BLACK);
				content3.setBackgroundColor(Color.BLACK);
				content4.setBackgroundColor(Color.BLACK);

				if(anakAnswer[indexDataButton] != 0){
					if(anakAnswer[4] != 0){
						for(int j=1;j<=cekJawaban(realAnswer,anakAnswer);j++){
							if(j%2 == 0){
								nilaiAnak++;
							}
							if(j == jumlahSoal){
								nilaiAnak = 3;
							}
						}
						hasilTestMembaca = "hasil yang di dapat" +cekJawaban(realAnswer,anakAnswer);
						if(dbStar.getJumlah(ListProfil.profileID, ListBab.idBab, kelasAnak, ListBelajar.idBacaan) == 0){
							dbStar.insertDataBintang(ListProfil.profileID, ListBab.idBab, kelasAnak, ListBelajar.idBacaan, nilaiAnak);
							data_profil.addProfile(ListProfil.profileID, ChooseKelas.kelasSiswa, ListBelajar.idBacaan, ListBab.idBab, cekJawaban(realAnswer,anakAnswer));
						}
						if(dbStar.getJumlah(ListProfil.profileID, ListBab.idBab, kelasAnak, ListBelajar.idBacaan) != 0){
							data_profil.addProfile(ListProfil.profileID, ChooseKelas.kelasSiswa, ListBelajar.idBacaan, ListBab.idBab, cekJawaban(realAnswer,anakAnswer));
							if(dbStar.getJumlah(ListProfil.profileID, ListBab.idBab, kelasAnak, ListBelajar.idBacaan) < nilaiAnak){
								dbStar.updateBintang(ListProfil.profileID, ListBab.idBab, kelasAnak, ListBelajar.idBacaan, nilaiAnak);
							}
						}
						Intent i = new Intent(TestActivityMembaca.this,ViewResultTest.class);
						startActivity(i);
					}
					else{
						
						Toast.makeText(getBaseContext(), "Indeks data" + indexRandomGambar[indexDataButton],
								Toast.LENGTH_LONG).show();

						if(indexDataButton != jumlahSoal){
							indexDataButton++;
						}
						img_test_membaca.setImageBitmap(Utilities.getImage(tp.getImageFromDatabase(indexRandomGambar[indexDataButton])));
						content1.setText(multipleData[indexDataButton][0]);
						content2.setText(multipleData[indexDataButton][1]);
						content3.setText(multipleData[indexDataButton][2]);
						content4.setText(multipleData[indexDataButton][3]);
					}
				}					

				else{
					Toast.makeText(getBaseContext(), "Indeks data yang kosong adalah" + indexDataButton,
							Toast.LENGTH_LONG).show();
				}
			}
		});
        
        
    }
    public int cekJawaban(int [] JawabanSistem,int [] JawabanSiswa){
    	int result = 0;
    	for(int i = 0;i<JawabanSistem.length;i++){
    		if(JawabanSiswa[i] == JawabanSistem[i]){
    			result++;
    		}
    	}
    	return result;
    }
}
